package org.sunrise.game.client.main;

import org.sunrise.game.client.client.SocketClient;
import org.sunrise.game.client.client.SocketClientManager;
import org.sunrise.game.client.gen.LoginProto;
import org.sunrise.game.client.gen.MapProto;
import org.sunrise.game.client.gen.TopicProto;
import org.sunrise.game.client.main.frame.MainWindow;
import org.sunrise.game.client.main.frame.MapFrame;
import org.sunrise.game.common.config.ConfigReader;
import org.sunrise.game.common.utils.Utils;

import javax.swing.*;
import java.util.Properties;
import java.util.Random;

public class ClientStartUp {
    public static void main(String[] args) throws InterruptedException {
        // args[0]:config path
        if (args.length == 0) {
            args = new String[] { "./config/client-config.properties" };
        }
        ConfigReader.loadConfig(args[0]);
        Properties properties = ConfigReader.getProp();
        if (properties == null) {
            return;
        }
        LoginManager.httpUrl = properties.getProperty("http.address") + ":" + properties.getProperty("http.port");
//        LoginManager.httpUrl = properties.getProperty("http.address"); //nginx代理

        SocketClientManager.socketType = properties.getProperty("client.socket");;
        SocketClient client = SocketClientManager.getInstance();

        client.initHttp();

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MainWindow.instance = new MainWindow();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
        while (true) {
            if (client.isStartSend()) {
                break;
            }
            Thread.sleep(1000);
        }
        // 定时ping
        new Thread(() -> {
            while (true) {
                client.sendMsg(TopicProto.MBasePacketData.newBuilder()
                        .setPacketType(TopicProto.TOPIC.TOPIC_TYPE_LOGIN)
                        .setPacketId(LoginProto.FROM_CLIENT.C2S_ClientPing_VALUE)
                        .build());
                client.setPingStartTime(System.currentTimeMillis());
                client.setSendCount(client.getSendCount() + 1);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        client.sendMsg(TopicProto.MBasePacketData.newBuilder()
                .setPacketType(TopicProto.TOPIC.TOPIC_TYPE_MAP)
                .setPacketId(MapProto.FROM_CLIENT.C2S_Enter_VALUE)
                .setPacketData(MapProto.MC2S_Enter.newBuilder().setId(1).build().toByteString()) // 假设进入地图ID为1
                .build());

        Random random = new Random();
        int size = MapFrame.instance.getMapSize();
//        new Thread(() -> {
//            while (true) {
//                ClientFrame.instance.moveTo(random.nextInt(size), random.nextInt(size));
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }).start();

        Utils.setShutdownHook(() -> {
            client.sendMsg(TopicProto.MBasePacketData.newBuilder()
                    .setPacketType(TopicProto.TOPIC.TOPIC_TYPE_MAP)
                    .setPacketId(MapProto.FROM_CLIENT.C2S_Leave_VALUE)
                    .setPacketData(MapProto.MC2S_Leave.newBuilder().setId(1).build().toByteString())
                    .build());
        });
    }
}
